#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import unittest

import redirect as app


class GaeTestCase(unittest.TestCase):
  def setUp(self, *args, **kwargs):
    self.clear_datastore()
    super(GaeTestCase, self).setUp(*args, **kwargs)

  @staticmethod
  def clear_datastore():
    from google.appengine.api import apiproxy_stub_map, datastore_file_stub
    from google.appengine.api import memcache

    # See http://code.google.com/p/gaeunit/issues/detail?id=15 for clue.
    for key in ['datastore', 'datastore_v3']:
      # W0212: 23,16:GaeTestCase.clear_datastore: Access to a protected member
      # _APIProxyStubMap__stub_map of a client class
      # pylint: disable=W0212
      # E1101: 50,16:GaeTestCase.clear_datastore: Instance of 'APIProxyStubMap'
      # has no '_APIProxyStubMap__stub_map' member
      # pylint: disable=E1101
      if key in apiproxy_stub_map.apiproxy._APIProxyStubMap__stub_map:
        # W0212: 24,12:GaeTestCase.clear_datastore: Access to a protected
        # member _APIProxyStubMap__stub_map of a client class
        # pylint: disable=W0212
        # E1101: 54,12:GaeTestCase.clear_datastore: Instance of
        # 'APIProxyStubMap' has no '_APIProxyStubMap__stub_map' member
        # pylint: disable=E1101
        del apiproxy_stub_map.apiproxy._APIProxyStubMap__stub_map[key]

    # Use a fresh stub datastore.
    stub = datastore_file_stub.DatastoreFileStub(
        app.APP_NAME, '/dev/null', '/dev/null')
    apiproxy_stub_map.apiproxy.RegisterStub('datastore', stub)

    # Flush memcache.
    memcache.flush_all()


class RedirectTestCase(GaeTestCase):
  def setUp(self):
    from google.appengine.ext import testbed
    self.testbed = testbed.Testbed()
    self.testbed.activate()
    self.testbed.init_user_stub()

  def tearDown(self):
    self.testbed.deactivate()

  def redirect_helper(self, path):
    from webtest import TestApp
    testapp = TestApp(app.application)
    response = testapp.get(path)
    self.assertEquals('302 Moved Temporarily', response.status)
    self.assertEquals('', response.body)
    return response

  def user_helper(self, email, uid):
    self.testbed.setup_env(
        USER_EMAIL = email,
        USER_ID = uid,
        USER_IS_ADMIN = '0',
        overwrite = True)

  def test_main_page_redirect_logged_out(self):
    self.assertEquals(app.users.get_current_user(), None)
    page = self.redirect_helper('/')
    self.assertEquals(page.headers['Location'],
                      app.users.create_login_url('http://localhost/'))

  def test_main_page_redirect_logged_in(self):
    self.user_helper('tester@gmail.com', '123')
    page = self.redirect_helper('/')
    self.assertEquals(page.headers['Location'],
        'http://code.google.com/p/chromium/issues')

  def test_main_page_redirect_logged_in_chromium(self):
    self.user_helper('tester@chromium.org', '123')
    page = self.redirect_helper('/')
    self.assertEquals(page.headers['Location'],
        'http://chromiumbugs.appspot.com')

  def test_dev_page_redirect_logged_out(self):
    self.user_helper('', '')
    page = self.redirect_helper('/detailed')
    self.assertEquals(page.headers['Location'],
        'http://code.google.com/p/chromium/issues/entry')

  def test_dev_page_redirect_logged_in(self):
    self.user_helper('tester@gmail.com', '123')
    page = self.redirect_helper('/detailed')
    self.assertEquals(page.headers['Location'],
        'http://code.google.com/p/chromium/issues/entry')
